package com.example.design.patterns.冰河.行为模式.迭代器模式.concreteIterator;

import com.example.design.patterns.冰河.行为模式.迭代器模式.aggregate.List;
import com.example.design.patterns.冰河.行为模式.迭代器模式.iterator.Iterator;

/**
 * ConcreteIterator 具体迭代器实现迭代器接口.对于该聚合遍历时跟踪当前位置.
 */
public class IteratorImpl implements Iterator {
    private List list;
    private int index;

    public IteratorImpl(List list) {
        this.index = 0;
        this.list = list;
    }

    /**
     *
     * @return
     */
    @Override
    public Object next() {
        Object obj = list.get(index);
        index++;
        return obj;
    }

    /**
     *
     */
    @Override
    public void first() {
        this.index = 0;
    }

    /**
     *
     */
    @Override
    public void last() {
        this.index = list.getSize();
    }

    /**
     *
     * @return
     */
    @Override
    public boolean hasNext() {
        return index < list.getSize();
    }
}
